package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 虚拟钱包交易流水记录
 */
@Entity
@Table(name = "virtual_wallet_transaction")
public class VirtualWalletTransaction extends BaseEntity {

    /**
     * 交易时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "transaction_time", nullable = false)
    private LocalDateTime transactionTime;

    /**
     * 交易金额
     */
    @Column(name = "amount", precision = 10, scale = 2, nullable = false)
    private BigDecimal amount;

    /**
     * 交易类型（使用TransactionType枚举）
     */
    @Column(name = "transaction_type", length = 30, nullable = false)
    private String transactionType;

    /**
     * 交易状态：SUCCESS（成功）、FAILED（失败）、PENDING（处理中）
     */
    @Column(name = "status", length = 20, nullable = false)
    private String status;

    /**
     * 钱包ID（主钱包）
     */
    @Column(name = "wallet_id", nullable = false)
    private Long walletId;

    /**
     * 转出钱包ID（用于转账）
     */
    @Column(name = "from_wallet_id")
    private Long fromWalletId;

    /**
     * 转入钱包ID（用于转账）
     */
    @Column(name = "to_wallet_id")
    private Long toWalletId;

    /**
     * 关联订单ID
     */
    @Column(name = "order_id")
    private Long orderId;

    /**
     * 交易前余额
     */
    @Column(name = "balance_before", precision = 10, scale = 2)
    private BigDecimal balanceBefore;

    /**
     * 交易后余额
     */
    @Column(name = "balance_after", precision = 10, scale = 2)
    private BigDecimal balanceAfter;

    /**
     * 手续费/返点金额
     */
    @Column(name = "bonus_or_fee", precision = 10, scale = 2)
    private BigDecimal bonusOrFee;

    /**
     * 交易描述
     */
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 交易流水号
     */
    @Column(name = "transaction_no", length = 50, unique = true)
    private String transactionNo;

    /**
     * 备注
     */
    @Column(name = "remark", length = 500)
    private String remark;

    public VirtualWalletTransaction() {
    }

    @PrePersist
    protected void onPrePersist() {
        if (this.transactionTime == null) {
            this.transactionTime = LocalDateTime.now();
        }
        if (this.transactionNo == null) {
            // 生成交易流水号：时间戳 + 钱包ID + 随机数
            this.transactionNo = "TXN" + System.currentTimeMillis() + 
                                (walletId != null ? walletId : 0) + 
                                ((int)(Math.random() * 10000));
        }
    }

    // Getters and Setters
    public LocalDateTime getTransactionTime() {
        return transactionTime;
    }

    public void setTransactionTime(LocalDateTime transactionTime) {
        this.transactionTime = transactionTime;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getTransactionType() {
        return transactionType;
    }

    public void setTransactionType(String transactionType) {
        this.transactionType = transactionType;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Long getWalletId() {
        return walletId;
    }

    public void setWalletId(Long walletId) {
        this.walletId = walletId;
    }

    public Long getFromWalletId() {
        return fromWalletId;
    }

    public void setFromWalletId(Long fromWalletId) {
        this.fromWalletId = fromWalletId;
    }

    public Long getToWalletId() {
        return toWalletId;
    }

    public void setToWalletId(Long toWalletId) {
        this.toWalletId = toWalletId;
    }

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public BigDecimal getBalanceBefore() {
        return balanceBefore;
    }

    public void setBalanceBefore(BigDecimal balanceBefore) {
        this.balanceBefore = balanceBefore;
    }

    public BigDecimal getBalanceAfter() {
        return balanceAfter;
    }

    public void setBalanceAfter(BigDecimal balanceAfter) {
        this.balanceAfter = balanceAfter;
    }

    public BigDecimal getBonusOrFee() {
        return bonusOrFee;
    }

    public void setBonusOrFee(BigDecimal bonusOrFee) {
        this.bonusOrFee = bonusOrFee;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTransactionNo() {
        return transactionNo;
    }

    public void setTransactionNo(String transactionNo) {
        this.transactionNo = transactionNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * 创建交易记录的建造者模式
     */
    public static class Builder {
        private VirtualWalletTransaction transaction;

        public Builder() {
            transaction = new VirtualWalletTransaction();
            transaction.setTransactionTime(LocalDateTime.now());
            transaction.setStatus("SUCCESS");
        }

        public Builder walletId(Long walletId) {
            transaction.setWalletId(walletId);
            return this;
        }

        public Builder amount(BigDecimal amount) {
            transaction.setAmount(amount);
            return this;
        }

        public Builder transactionType(TransactionType type) {
            transaction.setTransactionType(type.getCode());
            return this;
        }

        public Builder status(String status) {
            transaction.setStatus(status);
            return this;
        }

        public Builder fromWalletId(Long fromWalletId) {
            transaction.setFromWalletId(fromWalletId);
            return this;
        }

        public Builder toWalletId(Long toWalletId) {
            transaction.setToWalletId(toWalletId);
            return this;
        }

        public Builder orderId(Long orderId) {
            transaction.setOrderId(orderId);
            return this;
        }

        public Builder balanceBefore(BigDecimal balanceBefore) {
            transaction.setBalanceBefore(balanceBefore);
            return this;
        }

        public Builder balanceAfter(BigDecimal balanceAfter) {
            transaction.setBalanceAfter(balanceAfter);
            return this;
        }

        public Builder bonusOrFee(BigDecimal bonusOrFee) {
            transaction.setBonusOrFee(bonusOrFee);
            return this;
        }

        public Builder description(String description) {
            transaction.setDescription(description);
            return this;
        }

        public Builder remark(String remark) {
            transaction.setRemark(remark);
            return this;
        }

        public VirtualWalletTransaction build() {
            return transaction;
        }
    }
}
